/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.ContactGroup;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class GetContacts
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   private static final int ALL_FOLDERS = -1;
/*     */   static final long NO_LIMIT_MAX_MEMBERS = 0L;
/*     */   private static final long DEFAULT_MAX_MEMBERS = 0L;
/*  50 */   protected static final String[] TARGET_FOLDER_PATH = { "l" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  54 */     return TARGET_FOLDER_PATH;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  59 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  60 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  61 */     OperationContext octxt = getOperationContext(zsc, context);
/*  62 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  64 */     boolean sync = request.getAttributeBool("sync", false);
/*  65 */     boolean derefContactGroupMember = request.getAttributeBool("derefGroupMember", false);
/*     */     
/*  67 */     String folderIdStr = request.getAttribute("l", null);
/*  68 */     int folderId = -1;
/*  69 */     if (folderIdStr != null) {
/*  70 */       ItemId iidFolder = new ItemId(folderIdStr, zsc);
/*  71 */       if (iidFolder.belongsTo(mbox)) {
/*  72 */         folderId = iidFolder.getId();
/*     */       } else {
/*  74 */         throw ServiceException.FAILURE("Got remote folderId: " + folderIdStr + " but did not proxy", null);
/*     */       }
/*     */     }
/*  77 */     SortBy sort = SortBy.of(request.getAttribute("sortBy", null));
/*  78 */     if (sort == null) {
/*  79 */       sort = SortBy.NONE;
/*     */     }
/*  81 */     ArrayList<String> attrs = null;
/*  82 */     ArrayList<String> memberAttrs = null;
/*  83 */     ArrayList<ItemId> ids = null;
/*     */     
/*  85 */     for (Element e : request.listElements()) {
/*  86 */       if (e.getName().equals("a")) {
/*  87 */         String name = e.getAttribute("n");
/*  88 */         if (attrs == null)
/*  89 */           attrs = new ArrayList();
/*  90 */         attrs.add(name);
/*  91 */       } else if (e.getName().equals("ma")) {
/*  92 */         String name = e.getAttribute("n");
/*  93 */         if (memberAttrs == null)
/*  94 */           memberAttrs = new ArrayList();
/*  95 */         memberAttrs.add(name);
/*  96 */       } else if (e.getName().equals("cn")) {
/*  97 */         String idStr = e.getAttribute("id");
/*  98 */         String[] targets = idStr.split(",");
/*  99 */         for (String target : targets) {
/* 100 */           ItemId iid = new ItemId(target, zsc);
/* 101 */           if (ids == null)
/* 102 */             ids = new ArrayList();
/* 103 */           ids.add(iid);
/*     */         }
/*     */         
/*     */ 
/* 107 */         e.detach();
/*     */       }
/*     */     }
/*     */     
/* 111 */     long maxMembers = 0L;
/* 112 */     boolean returnHiddenAttrs = false;
/* 113 */     if (attrs == null) {
/* 114 */       returnHiddenAttrs = request.getAttributeBool("returnHiddenAttrs", false);
/* 115 */       maxMembers = request.getAttributeLong("maxMembers", 0L);
/*     */     }
/*     */     
/* 118 */     Element response = zsc.createElement(MailConstants.GET_CONTACTS_RESPONSE);
/*     */     
/*     */ 
/* 121 */     int fields = -4194305;
/* 122 */     if (sync) {
/* 123 */       fields |= 0x400000;
/*     */     }
/*     */     
/* 126 */     if ((derefContactGroupMember) && 
/* 127 */       (ids == null)) {
/* 128 */       throw ServiceException.INVALID_REQUEST("derefGroupMember is supported only when specific contact ids are specified", null);
/*     */     }
/*     */     
/*     */     boolean migrateDlist;
/*     */     Iterator i$;
/* 133 */     if (ids != null) {
/* 134 */       ArrayList<Integer> local = new ArrayList();
/* 135 */       HashMap<String, StringBuffer> remote = new HashMap();
/* 136 */       partitionItems(zsc, ids, local, remote);
/*     */       
/* 138 */       if (remote.size() > 0) {
/* 139 */         if (folderId > 0) {
/* 140 */           throw ServiceException.INVALID_REQUEST("Cannot specify a folder with mixed-mailbox items", null);
/*     */         }
/* 142 */         List<Element> responses = proxyRemote(request, remote, context);
/* 143 */         for (Element e : responses) {
/* 144 */           response.addElement(e);
/*     */         }
/*     */       }
/* 147 */       if (local.size() > 0) {
/* 148 */         migrateDlist = CreateContact.needToMigrateDlist(zsc);
/* 149 */         for (i$ = local.iterator(); i$.hasNext();) { int id = ((Integer)i$.next()).intValue();
/* 150 */           Contact con = mbox.getContactById(octxt, id);
/* 151 */           if ((con != null) && ((folderId == -1) || (folderId == con.getFolderId()))) {
/* 152 */             ContactGroup contactGroup = null;
/* 153 */             String migratedDlist = null;
/* 154 */             if (migrateDlist) {
/* 155 */               ContactGroup cg = ContactGroup.init(con, false);
/* 156 */               if (cg != null) {
/* 157 */                 migratedDlist = cg.migrateToDlist(con.getMailbox(), octxt);
/*     */               }
/* 159 */             } else if (derefContactGroupMember) {
/* 160 */               contactGroup = ContactGroup.init(con, false);
/* 161 */               if (contactGroup != null) {
/* 162 */                 contactGroup.derefAllMembers(con.getMailbox(), octxt, zsc.getResponseProtocol());
/*     */               }
/*     */             }
/*     */             
/* 166 */             ToXML.encodeContact(response, ifmt, octxt, con, contactGroup, memberAttrs, false, attrs, fields, migratedDlist, returnHiddenAttrs, maxMembers);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     else
/*     */     {
/* 173 */       for (Contact con : mbox.getContactList(octxt, folderId, sort)) {
/* 174 */         if (con != null) {
/* 175 */           ToXML.encodeContact(response, ifmt, octxt, con, null, null, false, attrs, fields, null, returnHiddenAttrs, maxMembers);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 180 */     return response;
/*     */   }
/*     */   
/*     */   static void partitionItems(ZimbraSoapContext lc, ArrayList<ItemId> ids, ArrayList<Integer> local, Map<String, StringBuffer> remote)
/*     */     throws ServiceException
/*     */   {
/* 186 */     Account acct = getRequestedAccount(lc);
/* 187 */     for (ItemId iid : ids) {
/* 188 */       if (iid.belongsTo(acct)) {
/* 189 */         local.add(Integer.valueOf(iid.getId()));
/*     */       } else {
/* 191 */         StringBuffer sb = (StringBuffer)remote.get(iid.getAccountId());
/* 192 */         if (sb == null) {
/* 193 */           remote.put(iid.getAccountId(), new StringBuffer(iid.toString()));
/*     */         } else {
/* 195 */           sb.append(',').append(iid.toString());
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   List<Element> proxyRemote(Element request, Map<String, StringBuffer> remote, Map<String, Object> context) throws ServiceException {
/* 202 */     List<Element> responses = new ArrayList();
/*     */     
/*     */ 
/* 205 */     Element cn = request.addElement("cn");
/* 206 */     for (Map.Entry<String, StringBuffer> entry : remote.entrySet()) {
/* 207 */       cn.addAttribute("id", ((StringBuffer)entry.getValue()).toString());
/*     */       
/* 209 */       Element response = proxyRequest(request, context, (String)entry.getKey());
/* 210 */       for (Element e : response.listElements()) {
/* 211 */         responses.add(e.detach());
/*     */       }
/*     */     }
/* 214 */     return responses;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/GetContacts.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */